package com.dadibadi.core.work.controller;

import com.dadibadi.core.common.exception.MyBizException;
import com.dadibadi.core.dao.entity.WorkInfoTab;
import com.dadibadi.core.dao.mapper.WorkInfoMapper;
import com.dadibadi.core.user.qo.UserInfoEditQO;
import com.dadibadi.core.user.service.UserInfoService;
import com.dadibadi.core.work.qo.WorkInfoIdQO;
import com.venusoft.developer.common.result.CommonResult;
import com.venusoft.developer.common.result.ICommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * TODO
 *
 * @author : lkfs@163.com
 * @date : 2025-06-10 14:11
 * 根据id删除作品,如果此用户id与作品的作者id相同，则可以删除
 */

@RestController
@RequestMapping("/work")
@Slf4j
@Tag(name = "api/作品信息", description = "")
public class ApiWorkInfoDeleteController {
    @Resource
    private WorkInfoMapper workInfoMapper;
    @Resource
    private UserInfoService userInfoService;

    @PostMapping("/api-work-info-delete")
    @Operation(summary = "用户删除作品", description = "")
    public ICommonResult exec(
            @RequestBody @Parameter(required = true) @Validated WorkInfoIdQO qo,
            @RequestHeader(required = false) String platform,
            @RequestHeader(required = false) String version,
            HttpServletRequest request) {
        WorkInfoTab workInfoTab = new WorkInfoTab();
        workInfoTab = workInfoMapper.selectById(qo.getWorkId());
        if (workInfoTab.getUserId().equals(qo.getUserId())) {
            workInfoMapper.deleteById(qo.getWorkId());
            UserInfoEditQO userInfoEditQO= UserInfoEditQO.builder()
                    .userId(qo.getUserId())
                    .build();
            userInfoService.updateArticleCount(userInfoEditQO);
            return new CommonResult<>("success");
        } else {
            throw MyBizException.NOT_THE_AUTHOR;
        }


    }
}
